package com.b3log.baidu.pcs.common;

public class ByteBuffer {
    
    public ByteBuffer() {
        this(16);
    }
    
    public ByteBuffer(int intCapacity) {
        if (intCapacity < 0){
            intCapacity = 0;
        }
        cintDataLength = 0;
        cintDataCapacity = intCapacity;
        cbyteData = new byte[cintDataCapacity];
        cintGrowthBytes = cintDataCapacity / 10;
        if (cintGrowthBytes < 16){
            cintGrowthBytes = 16;
        }
        cdoubleGrowthPercent = 10D;
    }
    
    public ByteBuffer append(byte byteData) {
        ensureCapacity(cintDataLength + 1);
        cbyteData[cintDataLength++] = byteData;
        return this;
    }
    
    private void arraycopy(byte bytesSource[], int intSourceIndex, byte bytesDestination[], int intDestinationIndex,
            int intCount) {
        if (intSourceIndex >= intDestinationIndex) {
            for (int intCounter = 0; intCounter < intCount; intCounter++)
                bytesDestination[intDestinationIndex++] = bytesSource[intSourceIndex++];
            
        } else {
            intDestinationIndex += intCount;
            intSourceIndex += intCount;
            for (int intCounter = 0; intCounter < intCount; intCounter++)
                bytesDestination[--intDestinationIndex] = bytesSource[--intSourceIndex];
            
        }
    }
    
    public int capacity() {
        return cintDataCapacity;
    }
    
    public void ensureCapacity(int intCapacity) {
        if (cintDataCapacity < intCapacity) {
            int intGrowthBytes = intCapacity - cintDataCapacity;
            if (intGrowthBytes < cintGrowthBytes)
                intGrowthBytes = cintGrowthBytes;
            int intGrowthPercentBytes = (int) (((double) cintDataLength * cdoubleGrowthPercent) / 100D);
            if (intGrowthBytes < intGrowthPercentBytes)
                intGrowthBytes = intGrowthPercentBytes;
            cintDataCapacity = cintDataCapacity + intGrowthBytes;
            byte byteDataNew[] = new byte[cintDataCapacity];
            arraycopy(cbyteData, 0, byteDataNew, 0, cintDataLength);
            cbyteData = byteDataNew;
        }
    }
    
    public byte[] getBytes() {
        return toBytes();
    }
    
    public byte[] getBytes(int intPos, int intLength) {
        byte byteData[] = new byte[intLength];
        arraycopy(cbyteData, intPos, byteData, 0, intLength);
        return byteData;
    }
    
    public int length() {
        return cintDataLength;
    }
    
    public byte[] toBytes() {
        byte byteData[] = new byte[cintDataLength];
        arraycopy(cbyteData, 0, byteData, 0, cintDataLength);
        return byteData;
    }
    
    public String toString() {
        return new String(cbyteData, 0, cintDataLength);
    }
    
    byte cbyteData[];
    
    int cintDataCapacity;
    
    int cintDataLength;
    
    int cintGrowthBytes;
    
    double cdoubleGrowthPercent;
}